﻿
Imports osi.root.connector

Public Module _array_pointer
    Public Function make_array_pointer(Of T)(ByVal i() As T) As array_pointer(Of T)
        Return New array_pointer(Of T)(i)
    End Function
End Module

Public Class array_pointer(Of T)
    Inherits pointer(Of T())
    Implements IComparable(Of array_pointer(Of T))

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal i() As T)
        MyBase.New(i)
    End Sub

    Public Sub New(ByVal i As _pointer(Of T()))
        MyBase.New(i)
    End Sub

    Protected NotOverridable Overrides Function compare(ByVal i() As T, ByVal j() As T) As Int32
        Return memcmp(i, j)
    End Function

    Public Overloads Function CompareTo(ByVal other As array_pointer(Of T)) As Int32 _
                                       Implements IComparable(Of array_pointer(Of T)).CompareTo
        Return MyBase.CompareTo(other)
    End Function

    Public Overrides Function GetHashCode() As Int32
        Dim i As T() = Nothing
        i = [get]()
        If isemptyarray(i) Then
            Return 0
        Else
            'WTF, the Array.GetHashCode returns some random number in .net 3.5
            Dim r As Int32 = 0
            For j As Int32 = 0 To min(32, array_size(i)) - 1
                If i(j) Is Nothing Then
                    r = r Xor 0
                Else
                    r = r Xor i(j).GetHashCode()
                End If
            Next
            Return r
        End If
    End Function
End Class
